package com.niodata.md.rdb.model;

import java.util.List;
import lombok.Getter;
import lombok.Setter;

/**
 * rdb catalog info.
 */
@Getter
@Setter
public class Catalog {
  private DbInfo dbInfo;
  private String name;
  private List<DataBase> dataBaseList;

  /**
   * constructor.
   */
  public Catalog() {

  }

  /**
   * get exact data base by data base path.
   *
   * @param dataBasePath data base path identified by catalog, data base name
   * @return database
   */
  public DataBase getDataBase(String[] dataBasePath) {
    if (dataBasePath.length < 2) {
      throw new RuntimeException("path error:" + dataBasePath);
    }
    if (!dataBasePath[0].equals(name)) {
      throw new RuntimeException("path error: catalog name error");
    }
    for (DataBase dataBase : dataBaseList) {
      if (dataBase.getName().equals(dataBasePath[1])) {
        return dataBase;
      }
    }
    return null;
  }

  /**
   * get exact table by table path.
   *
   * @param tablePath table path  identified by catalog, schema, table name
   * @return table
   */
  public Table getTable(String[] tablePath) {
    if (tablePath.length < 3) {
      throw new RuntimeException("path error:" + tablePath);
    }
    for (DataBase dataBase : dataBaseList) {
      if (dataBase.getName().equals(tablePath[1])) {
        for (Table table : dataBase.getTables()) {
          if (table.getName().equals(tablePath[2])) {
            return table;
          }
        }
      }
    }
    return null;
  }


  /**
   * get exact view by view path.
   *
   * @param viewPath view path  identified by catalog, schema, view name
   * @return View
   */
  public View getView(String[] viewPath) {
    if (viewPath.length < 3) {
      throw new RuntimeException("path error:" + viewPath);
    }
    for (DataBase dataBase : dataBaseList) {
      if (dataBase.getName().equals(viewPath[1])) {
        for (View view : dataBase.getViews()) {
          if (view.getName().equals(viewPath[2])) {
            return view;
          }
        }
      }
    }
    return null;
  }


  /**
   * get exact column by column path.
   *
   * @param columnPath column path identified by catalog, schema, table/view, column name
   * @return column
   */
  public Column getColumn(String[] columnPath) {
    if (columnPath.length < 4) {
      throw new RuntimeException("path error:" + columnPath);
    }
    for (DataBase dataBase : dataBaseList) {
      if (dataBase.getName().equals(columnPath[1])) {
        for (Table table : dataBase.getTables()) {
          if (table.getName().equals(columnPath[2])) {
            for (Column column : table.getColumns()) {
              if (column.getName().equals(columnPath[3])) {
                return column;
              }
            }
          }
        }
      }
    }
    return null;
  }


  /**
   * get exact index by index path.
   *
   * @param indexPath index path identified by catalog, schema, table, index name
   * @return index
   */
  public Index getIndex(String[] indexPath) {
    if (indexPath.length < 4) {
      throw new RuntimeException("path error:" + indexPath);
    }
    for (DataBase dataBase : dataBaseList) {
      if (dataBase.getName().equals(indexPath[1])) {
        for (Table table : dataBase.getTables()) {
          if (table.getName().equals(indexPath[2])) {
            for (Index index : table.getIndices()) {
              if (index.getName().equals(indexPath[3])) {
                return index;
              }
            }
          }
        }
      }
    }
    return null;
  }
}
